package com.test08_面向对象应用;

import org.junit.Test;

public class YootkDemo_01 {

    @Test
    public void test1() {
        {   // 第一组判断
            Book book = new Book();
            System.out.println(book instanceof Book);
            System.out.println(book instanceof MathBook);
        }
        System.out.println("===================================");
        {   // 第二组判断
            Book book = new MathBook();
            System.out.println(book instanceof Book);
            System.out.println(book instanceof MathBook);
        }
    }

    @Test
    public void test2() {
        Book book = new MathBook();
        book.read();
        if (book instanceof MathBook) {
            MathBook math = (MathBook) book;
            math.calc();
        } else {
            System.out.println("拿的不是数学书");
        }
    }

    @Test
    public void test3() {
        Book book = null;   // 没有实例化对象
        System.out.println(book instanceof Book);
    }

    @Test
    public void test4() {
        // Object是所有类的父类
        Object obj = new Book();    // 向上转型
        if (obj instanceof Book) {
            Book book = (Book) obj; // 向下转型
            book.read();
        }
    }

    @Test
    public void test5() {
        // String向上转型
        Object obj = "沐言优拓：www.yootk.com";
        if (obj instanceof String) {
            String str = (String) obj;
            System.out.println(str.toUpperCase());
        }
    }

    @Test
    public void test6() {
        // 只要是引用数据类型，都可以向Object向上转型
        Object obj = new String[] {"www.youtk.com", "www.baidu.com", "www.taobao.com"};
        print(obj);
        print(new int[] {10, 30, 50});
    }

    /** 进行数组的输出 */
    public static void print(Object param) {
        if (param instanceof String[]) {
            String[] array = (String[]) param;
            for (String temp : array) {
                System.out.print(temp + "、");
            }
            System.out.println();
        }
        if (param instanceof int[]) {
            int[] array = (int[]) param;
            for (int temp : array) {
                System.out.print(temp + "、");
            }
            System.out.println();
        }
    }

    @Test
    public void test7() {
//        System.out.println(new Book());
//        System.out.println(new Book().toString());
        System.out.println(new Book("Java", "李兴华", 99));
        System.out.println("www.yootk.com");    // String重写了Object的toString方法
    }

    @Test
    public void test8() {
        Book bookA = new Book("Java", "李兴华", 99);
        Book bookB = new Book("Java", "李兴华", 99);
//        if (bookA.getTitle().equals(bookB.getTitle())
//                && bookA.getAuthor().equals(bookB.getAuthor())
//                && bookA.getPrice() == bookB.getPrice()) {
//            System.out.println("【√】内容相等");
//        } else {
//            System.out.println("【×】内容不相等");
//        }
        if (bookA.equals(bookB)) {
            System.out.println("【√】内容相等");
        } else {
            System.out.println("【×】内容不相等");
        }
        System.out.println("=======================");
        {
            System.out.println(bookA.equals("www.yootk.com"));
            System.out.println(bookA.equals(null));
        }
    }
}

class Book {
    private String title;
    private String author;
    private double price;

    public Book() {}

    public Book(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public void read() {
        System.out.println("【Book】认真学习");
    }

    public String getTitle() {
        return this.title;
    }

    public String getAuthor() {
        return this.author;
    }

    public double getPrice() {
        return this.price;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Book) {
            Book book = (Book) obj;
            return this.title.equals(book.getTitle()) && this.author.equals(book.getAuthor()) && this.price == book.getPrice();
        }
        return false;
    }

    @Override
    public String toString() {
        return "【Book- " + super.toString() + "】图书名称：" + this.title
                + "、图书作者：" + this.author
                + "、图书价格：" + this.price;
    }
}

class MathBook extends Book {
    public void calc() {
        System.out.println("【MathBook】学习四则运算");
    }
}
